package dao_manager;

import java.util.List;

import javax.persistence.NonUniqueResultException;
import javax.servlet.http.HttpServletRequest;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.exception.ConstraintViolationException;

import dao_hibernate.GenericDAOImpl;
import dao_hibernate.HibernateUtil;
import ExceptionPerso.DupplicatedEntry;
import beans.ClientInfo;
import beans.UserRights;
import beans.user_Info;

public class UserInfoManagerImpl implements UserInfoManager {

    private GenericDAOImpl<user_Info, Long> UserInfoDao = new GenericDAOImpl<user_Info, Long>() {
                                                        };
    user_Info                               user_info;

    @Override
    // TODO nonUniqueResult
    public user_Info findUserInfo( String userName ) throws NonUniqueResultException {

        try {
            HibernateUtil.beginTransaction();
            Query query = HibernateUtil.getSession().getNamedQuery( "getUserByID" );
            query.setString( 0, userName );
            user_info = UserInfoDao.findOne( query );
            if ( user_info != null )
            {
                Hibernate.initialize( user_info.getUserRights() );
            }
            HibernateUtil.commitTransaction();
        } catch ( Exception ex ) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        }
        return user_info;
    }

    @SuppressWarnings( "unchecked" )
    @Override
    public List<user_Info> getAllUser() {
        
        List<user_Info> listUser = null;
        try {
            HibernateUtil.beginTransaction();
            listUser = UserInfoDao.findAll( user_Info.class );
            HibernateUtil.commitTransaction();
        } catch ( Exception ex ) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        }
        return listUser;
    }

    @Override
    public void addClient( ClientInfo clientInfo, long userID, HttpServletRequest request ) {
        GenericDAOImpl<ClientInfo, Long> clientInfoDao = new GenericDAOImpl<ClientInfo, Long>() {};
        String ATT_USER_LIST_CLIENT ="list_client";
        try {
            HibernateUtil.beginTransaction();
            user_info = UserInfoDao.findByID( user_Info.class, userID );
            clientInfoDao.save(clientInfo);
            user_info.getListClient().add( clientInfo );
            request.getSession().setAttribute( ATT_USER_LIST_CLIENT, user_info.getListClient() );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        }
    }

    @Override
    public void saveNewUser( user_Info user_info, UserRights userRights ) throws DupplicatedEntry{
        GenericDAOImpl<UserRights, Long> userRightsDao = new GenericDAOImpl<UserRights, Long>() {};

        try {
            HibernateUtil.beginTransaction();
            user_info.setUserRights( userRights );
            userRightsDao.save( userRights );
            UserInfoDao.save( user_info );
            HibernateUtil.commitTransaction();
        } catch ( ConstraintViolationException e )
        {
            HibernateUtil.rollback();
            throw new DupplicatedEntry();
        } catch ( Exception ex ) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        }
        
    }

   


}
